package controller

import (
	"log"
	"server/common"
	"server/response"
	"server/service"
	"server/vo"

	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
)

type FormController struct {
	DB *gorm.DB
}

func NewFormController() FormController {
	db := common.GetDB()
	return FormController{DB: db}
}

// @Summary Submit form
// @Tags Form API
// @Accept json
// @Description Submit a form with contact details and requirements
// @Produce json
// @Param request body vo.FormSubmissionRequest true "Form submission data"
// @Success 200 {string} string "ok"
// @Router /form/submit [post]
func (c *FormController) SubmitForm(ctx *gin.Context) {
	var request vo.FormSubmissionRequest

	// Data validation
	if err := ctx.ShouldBind(&request); err != nil {
		log.Print(err.Error())
		response.Fail(ctx, "Data validation error", nil)
		return
	}

	// Create form submission service
	formService := service.NewFormService(c.DB)

	// Get client IP address
	clientIP := ctx.ClientIP()

	// Submit the form with client IP
	if err := formService.CreateFormSubmissionWithIP(request, clientIP); err != nil {
		response.Fail(ctx, "Form submission failed", nil)
		return
	}

	// Success
	response.Success(ctx, nil, "Form submitted successfully")
}
